package com.yichen.music;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.music.Utils.LyricGetUtil;
import com.yichen.util.Song_Info;

import android.app.Activity;
import android.app.AlertDialog;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.SlidingDrawer;
import android.widget.TextView;

public class MusicPlay extends Activity implements OnClickListener {

	String song_url = "";
	static int i = 0;
	MediaPlayer media_play;
	static int index = 0;// 数组的下标
	static int nowpro = 0;// 现在的进度
	TextView lrc_show;
	String song_id;
	String song_name;
	String actor_name;
	Song_Info song;// 歌曲的信息处理类
	Handler setlycHandler;
	ProgressBar pro;
	ArrayList<Object> aa = null;;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.music_play);

		song_id = getIntent().getStringExtra("song_id");
		actor_name = getIntent().getStringExtra("actor_name");
		song_name = getIntent().getStringExtra("song_name");

		song = new Song_Info();
		// aa = new LrcProcessor().process(new ByteArrayInputStream(text
		// .getBytes()));

		LyricGetUtil lrcdd = new LyricGetUtil();
		aa = lrcdd.getByName(song_name, actor_name, "lrc");

		Button btn = (Button) findViewById(R.id.start_btn);
		Button btn2 = (Button) findViewById(R.id.get_progress);
		pro = (ProgressBar) findViewById(R.id.progressBar1);
		lrc_show = (TextView) findViewById(R.id.lrc_show);
		media_play = new MediaPlayer();
		btn.setOnClickListener(this);

		setlycHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub

				System.out.println();
				pro.setProgress(media_play.getCurrentPosition() / 1000);
				setTitle(gettimeforseconds(media_play.getCurrentPosition()));
				// lrc_show.setText("时间："
				// + gettimeforseconds(media_play.getCurrentPosition()));
//				ss(media_play.getCurrentPosition());
				// ss((Integer) msg.obj);
				super.handleMessage(msg);
			}

		};
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		media_play.stop();
	}

	private String ss(int a) {

		// TODO Auto-generated method stub
		String lrc = "";
		if (a >= nowpro) {
			System.out.println("当前时间" + a);
			System.out.println("第" + index + "句歌词");
			index++;
			Map<String, Object> ma = new HashMap<String, Object>();
			if (index < aa.size()) {
				ma = (Map<String, Object>) aa.get(index);

				nowpro = Integer.parseInt(ma.get("timeMill").toString());
				System.out.println("下一句歌词的时间" + nowpro);
				lrc = (String) ma.get("content");
				System.out.println("下一句歌词:" + lrc);
				lrc_show.setText(lrc);
			} else {
				System.out.println("--------------------------歌词完结");

			}

		}

		return lrc;

	}

	// 将毫秒转换成时间
	private String gettimeforseconds(int sec) {
		String h = "0";
		String m = "0";
		String s = "0";
		// 总秒/分/时数
		int alls = sec / 1000;
		int allm = alls / 60;
		int allh = allm / 60;
		// 转换之后的时间各个位置
		int iss = alls % 60;
		int ism = allm % 60;
		int ish = allh;

		h = append_zero(ish);
		m = append_zero(ism);
		s = append_zero(iss);
		return h + ":" + m + ":" + s;
		// System.out.println(h + ":" + m + ":" + s);
	}

	// 给时间是10以内的加上0
	private String append_zero(int tt) {
		// TODO Auto-generated method stub
		String re_str = "";
		if (tt < 10) {
			re_str = "0" + tt;
		} else {
			re_str = "" + tt;
		}
		return re_str;

	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.start_btn:
			start_music();
			break;

		default:
			break;
		}

	}

	private void start_music() {
		// TODO Auto-generated method stub
		// System.out.println(song_id);
		song_url = song.getSong_infoById(song_id);//
		media_play = new MediaPlayer();

		try {
			media_play.setDataSource(song_url);
			media_play.prepare();
			media_play.start();

			// 设置歌词和进度
			setLrcText();

		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	// 设置歌词和进度
	private void setLrcText() {
		// TODO Auto-generated method stub
		new Thread(new Runnable() {

			public void run() {
				// TODO Auto-generated method stub
				try {

					System.out.println("_________++++++++++++++");
					while (true) {

						// i++;
						// media_play.getCurrentPosition();
						Thread.sleep(1);
						Message msg = new Message();
						// msg.obj = i;
						setlycHandler.sendMessage(msg);
					}

				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}).start();

	}

}
